<template>
	<view class="queAndAnswer">
		<uni-tabchange :tabArr="tabArr" :isFixed="true" :activeTab="activeTab" @tabChangeSuccess="tabChangeSuccess"></uni-tabchange>
		<template v-for="(item,index) in listData">
			<view class="quesOne" :key="index">
				<uni-question :quesItem="item"></uni-question>
			</view>
		</template>
		<uni-nodata v-if="listData.length==0" :iconStyle="{'margin':'100rpx 0 0'}"></uni-nodata>
	</view>
</template>

<script>
	import { myWaitQues,myAnswerQues,myQues } from "@/api/mine.js"
	export default {
		data() {
			return {
				activeTab:0,
				tabArr:[{
					title:'待回答'
				},{
					title:'已回答'
				},{
					title:'我提问'
				}],
				page:1,
				pageSize:10,
				pageTotal:0,
				listData:[]
			}
		},
		onLoad() {
			this.getTabList()
		},
		methods: {
			// 根据tab获取数据
			getTabList(){
				this.page=1;
				this.listData=[]
				let param={
					page:this.page,
					size:this.pageSize,
					userId:uni.getStorageSync('userId')
				}
				if(this.activeTab==0){
					this.getWaitList(param)
				}else if(this.activeTab==1){
					this.getAnswerList(param)
				}else if(this.activeTab==2){
					this.getQuesList(param)
				}
			},
			// 待回答
			getWaitList(param){
				myWaitQues(param).then((res)=>{
					if(res.code==100000){
						this.listData=this.listData.concat(res.data.rows)
						this.pageTotal=Math.ceil(res.data.total/this.pageSize)
					}
				})
			},
			// 已回答
			getAnswerList(param){
				myAnswerQues(param).then((res)=>{
					if(res.code==100000){
						this.listData=this.listData.concat(res.data.rows)
						this.pageTotal=Math.ceil(res.data.total/this.pageSize)
					}
				})
			},
			// 我提问
			getQuesList(param){
				myQues(param).then((res)=>{
					if(res.code==100000){
						this.listData=this.listData.concat(res.data.rows)
						this.pageTotal=Math.ceil(res.data.total/this.pageSize)
					}
				})
			},
			tabChangeSuccess(obj){
				console.log('obj',obj)
				this.activeTab=obj.activeTab
				this.getTabList()
			}
		},
		onPullDownRefresh() {
			this.getTabList()
			setTimeout(()=>{
				uni.stopPullDownRefresh()
			},1000)
		},
		onReachBottom() {
			if(this.page<this.pageTotal){
				++this.page
				let param={
					page:this.page,
					size:this.pageSize,
					userId:uni.getStorageSync('userId')
				}
				if(this.activeTab==0){
					this.getWaitList(param)
				}else if(this.activeTab==1){
					this.getAnswerList(param)
				}else if(this.activeTab==2){
					this.getQuesList(param)
				}
			}else{
				uni.showToast({
					title:'我也是有底线的',
					icon:'none'
				})
			}
		}
	}
</script>
<style lang="scss">
@import "@/static/public.scss";	
page{
	background-color:$pageBgColor;
}
</style>
<style scoped lang="scss">
.quesOne{
	margin:0 0 20rpx;
}
</style>

